import { reactive } from "vue";
// 暂时用不上

const setUserInfo = (data: any) => {
	userInfo.id = data.userId;
	userInfo.userName = data.userName;
	userInfo.depId = data.deptId;
	userInfo.deptName = data.deptName;
	// userInfo.power = data.power;
	userInfo.isLonginIn = true;
	console.debug("打印看一下：", data, userInfo);
};

const setMenu = (data: any) => {
	// console.log(data)
	userInfo.power = data
}

const logout = () => {
	userInfo.id = -1;
	userInfo.depId = -1;
	userInfo.userName = "";
	userInfo.deptName = "";
	userInfo.power = [];
	userInfo.isLonginIn = false;
	userInfo.setToken(null);
};

let userInfo = reactive<User>({
	id: -1,
	depId: -1,
	userName: "",
	deptName: "",
	power: [],
	isLonginIn: false,
	setUserInfo: setUserInfo,
	setMenu: setMenu,
	getToken: () => {
		return localStorage.getItem("TOKEN") || '';
	},
	setToken: (token) => {
		if (token) {
			localStorage.setItem("TOKEN", token);
		} else {
			localStorage.removeItem("TOKEN");
		}
	},
	logOut: logout,
	canOperator:(pageId:number,buttionId:number) => true,
});

export default function useUser(): User {
	userInfo.isLonginIn = (localStorage.getItem("TOKEN") ?? "").length > 0;
	return userInfo;
}

export interface User {
	id: number;
	userName: string;
	deptName: string;
	power: [];
	depId:number;
	isLonginIn: boolean;
	setUserInfo: (data: any) => void;
	setMenu: (data: any) => void;
	getToken: () => string | null;
	setToken: (token: string | null) => void;
	logOut: () => void;
	canOperator:(pageId:number,buttionId:number) => boolean;
}

export interface UserLoginPara {
	loginName: string;
	password: string;
}
